package com.woniuxy.handler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.pojo.Order_dgy;
import com.woniuxy.pojo.Room_dgy;
import com.woniuxy.pojo.Guest;
import com.woniuxy.pojo.OrderDetail_dgy;
import com.woniuxy.service.OrderDetailService;
import com.woniuxy.service.OrderService;
import com.woniuxy.service.RoomService;

/**
 * 宾客退房
 * @author 邓国誉
 *
 */
@Controller
@RequestMapping("/check")
@Transactional
public class CheckOutHandler {
	
	@Resource
	private OrderService orderService;
	
	@Resource
	private OrderDetailService orderDetailService;
	
	@Resource
	private RoomService roomService;
	
	@RequestMapping("/out")
	public String checkOut(Order_dgy order,HttpServletRequest request){
		Room_dgy room = new Room_dgy();
		HttpSession session = request.getSession();
		order = (Order_dgy) session.getAttribute("order");
		if(order==null){
			return "html/offline.html";
		}
		order.setOstate("已退房");
//		BigDecimal totalPrice = order_dgy.getTotalprice();
/*		BigDecimal sum = new BigDecimal(0);
		List<OrderDetail_dgy> orderDetails =order.getOrderDetails();
		for(OrderDetail_dgy orderDetail : orderDetails){
			orderDetail.setScheckout(date);
			sum = sum.add(orderDetail.getNewprice());
			orderDetailService.updateOrderDetail(orderDetail);
		}*/
		//该退的价格
//		BigDecimal remainPrice = totalPrice.subtract(sum);
		System.out.println(order);
		List<OrderDetail_dgy> detail_dgies = order.getOrderDetails();
		for(OrderDetail_dgy detail_dgy : detail_dgies){
			room.setRid(detail_dgy.getRid());
			room.setRstate("需要打扫");
			roomService.updateRoomState(room);		
			orderDetailService.updateOrderDetail(detail_dgy);
		}
		orderService.changeOrder(order);
		return "html/offline.html";
	}
	
	@RequestMapping("/order")
	@ResponseBody
	public Order_dgy findOrderDetail(String gname,String gidcard,HttpServletRequest request) throws ParseException{
		Date date = new Date();
		long scheckOut = date.getTime();
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		HttpSession session = request.getSession();
		Guest guest = new Guest();
		guest.setGname(gname);
		guest.setGidcard(gidcard);
		System.err.println(guest);
		String oid = orderDetailService.findOidByGuest(guest);
		System.out.println(oid);
		Order_dgy order = orderService.findById(oid);
		if(order!=null){
			String scheckout = simpleDateFormat.format(date);
			List<OrderDetail_dgy> detail_dgies = order.getOrderDetails();
			System.err.println(detail_dgies);
			for(OrderDetail_dgy detail_dgy : detail_dgies){
				detail_dgy.setScheckout(scheckout);
			}
			Date checkOut=simpleDateFormat.parse(order.getOrderDetails().get(0).getCheckout());
			if(scheckOut>checkOut.getTime()){
				
			}
		}
		session.setAttribute("order", order);
		return order;
	}

}